﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ECG.Common;

namespace ECG.DAL
{
    public partial class ProductTagDb 
    {       
        /// <summary>
        /// Lấy về danh sách các Tags của Product
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static List<ProductTagDto> GetProductTagByProductId(string productId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductTagMapping_GetByProductId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductId", productId));
                return dao.GetDTOList<ProductTagDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy danh sách sản phẩm theo ProductTagId
        /// </summary>
        /// <param name="productTagId"></param>
        /// <param name="currentPage"></param>
        /// <param name="itemsPerPage"></param>
        /// <param name="totalpage"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public static List<ProductRateByCateTopListPagingDto> GetProductListByTag(int productTagId, int currentPage, int itemsPerPage, out int totalpage, out int totalRecords)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Product_GetAllByTag");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductTagId", productTagId));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                command.Parameters.Add(dao.CreateParameter("@CurrentPage", currentPage));

                var total_page = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                total_page.Direction = ParameterDirection.Output;

                var total_record = command.Parameters.Add("@TotalRecords", SqlDbType.Int);
                total_record.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<ProductRateByCateTopListPagingDto>(ref command);

                totalpage = int.Parse(total_page.Value.ToString() == "" ? "0" : total_page.Value.ToString());
                totalRecords = int.Parse(total_record.Value.ToString() == "" ? "0" : total_record.Value.ToString());

                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về tên của Tag theo Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetNameByProductTagId(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductTag_GetNameById");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", id));
                return dao.GetSingleString(command);
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
